package com.example.myapplication.Intelligent;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Config.DistrictTool;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ClickqueryBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IntelligentcontrolActivity extends AppCompatActivity {

    private ImageView img;
    private TextView submie;
    private EditText engineNumber;
    private TextView cat_type;
    private String TAG = "TAG";
    private Spinner sprnner;
    private String values;
    private String cattype;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intelligentcontrol);
        initview();
        //返回
        but_bloak();
        //点击选择汽车类
        selectcattype();
        //点击选择归属地
        selecthomeplace();
        //点击查询
        clickquery();

    }

    private void selecthomeplace() {
        Map<String, String> stringStringMap = DistrictTool.addDistrict();
        Set<String> strings = stringStringMap.keySet();
        List<String> list = new ArrayList();
        for (String string : strings) {
            list.add(string);
        }
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.item_sprnner, list);
        //设置下拉框的数组适配器
        sprnner.setAdapter(arrayAdapter);
        //设置数组适配器的布局样式
        //  arrayAdapter.setDropDownViewResource(R.layout.item_dropdown);
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        sprnner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                values = stringStringMap.get(list.get(position));
                //Toast.makeText(IntelligentcontrolActivity.this, "您选择的是：" + values, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void clickquery() {
        submie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String engineNumberString = engineNumber.getText().toString();
                String licencePlate = values + engineNumberString;
                if (TextUtils.isEmpty(cattype)) {
                    Toast.makeText(IntelligentcontrolActivity.this, "请选择车的类型", Toast.LENGTH_SHORT).show();
                }


                if ("".equals(engineNumberString) || TextUtils.isEmpty(engineNumberString)) {
                    Toast.makeText(IntelligentcontrolActivity.this, "车牌号不能为空", Toast.LENGTH_SHORT).show();
                }

                if (!TextUtils.isEmpty(licencePlate) && !"".equals(engineNumberString) && !TextUtils.isEmpty(engineNumberString) && !TextUtils.isEmpty(cattype)) {
                    OKHttpTool.doNetRequest("prod-api/api/traffic/illegal/list?catType=" + cattype + "&engineNumber=" + engineNumberString + "&licencePlate=" + licencePlate,
                            OKHttpTool.GET,
                            ClickqueryBean.class,
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            ClickqueryBean clickqueryBean = (ClickqueryBean) obj;
                                            if (clickqueryBean.getTotal() == 0) {
                                                Toast.makeText(IntelligentcontrolActivity.this, "暂无违章情况", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String msg) {

                                }
                            }
                    );
                }
            }
        });
    }

    private void selectcattype() {
        cat_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(IntelligentcontrolActivity.this);
                View inflate = LayoutInflater.from(IntelligentcontrolActivity.this).inflate(R.layout.item_cattype, null);
                RadioGroup ragroup = inflate.findViewById(R.id.ragroup);

                builder.setView(inflate);
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int checkedRadioButtonId = ragroup.getCheckedRadioButtonId();
                        cattype = "";
                        if (checkedRadioButtonId == R.id.rabut1) {
                            cattype = "大型汽车";
                        } else if (checkedRadioButtonId == R.id.rabut2) {
                            cattype = "小型汽车";
                        } else if (checkedRadioButtonId == R.id.rabut3) {
                            cattype = "大型新能源";
                        } else if (checkedRadioButtonId == R.id.rabut4) {
                            cattype = "小型新能源";
                        } else if (checkedRadioButtonId == R.id.rabut5) {
                            cattype = "领馆汽车";
                        } else if (checkedRadioButtonId == R.id.rabut6) {
                            cattype = "警用汽车";
                        }
                        cat_type.setText(cattype);
                    }
                });
                builder.create().show();
            }
        });
    }

    private void but_bloak() {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntelligentcontrolActivity.this.finish();
            }
        });
    }

    private void initview() {
        img = findViewById(R.id.img);
        cat_type = findViewById(R.id.cat_type);
        engineNumber = findViewById(R.id.engineNumber);
        submie = findViewById(R.id.submie);
        sprnner = findViewById(R.id.sprnner);
    }
}
